<?php

class HasManyAssociation extends Association
{

  /**
   *
   * @var boolean
   */
  protected $saveAfter = true;

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getName()
   */
  public function getName()
  {
    if (empty($this->name)) {
      return Inflector::pluralize(parent::getName());
    }

    return parent::getName();
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/api/IAssociationDefinition#getPrimaryKey()
   */
  public function getForeignKey()
  {
    if (empty($this->foreignKey)) {
      return $this->getTarget()->getName() . '_' . $this->getPrimaryKey();
    }

    return parent::getForeignKey();
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/domain/Association#loadData($client)
   */
  protected function loadData(IAggregate $client)
  {
    $def = $this->getDefinition();

    $dao = DAO::getInstance();

    $table = $def->getTarget()->getTable();
    $fk = $def->getForeignKey();
    $id = $client->getId();

    $data = $dao->query()->from($table)->where("$fk = ?")->execute($id)->fetchAll();

    $objs = DomainMapper::inflateCollection($def->getTarget(), $data);

    $meta = & $this->data($client);
    $meta['value'] = $objs;
  }

}

?>